from flask import Blueprint, request, render_template, current_app, make_response

from com.gen_captcha import get_captcha_code_and_content
from orms import ArticleORM, CategoryORM, CommentORM, CommentLikeORM
from flask_jwt_extended import create_access_token, set_access_cookies
from extensions import db, redis_db
from orms import UserORM

admin_api = Blueprint('admin', __name__)


# 返回用户数据接口
@admin_api.get('/user')
def api_user():
    page = request.args.get('page', default=1, type=int)
    per_page = request.args.get('per_page', default=10, type=int)

    q = db.select(UserORM)
    pag = db.paginate(q, page=page, per_page=per_page, error_out=False)
    user_list: list[UserORM] = pag.items
    return {
        "code": 0,
        'message': '查询数据成功',
        'data': [user.to_dict() for user in user_list]
    }


@admin_api.delete('/user/<int:uid>')
def api_user_delete(uid):
    # q = db.
    user: UserORM = UserORM.query.get(UserORM.id == uid)
    db.session.delete(user)
    db.session.commit()
    return {
        'code': 0,
        'message': '删除信息成功'
    }


#
# @admin_api.get('/article_list')
# def article_list():
#     return render_template('admin/article_list.html')


@admin_api.get('/article')
def get_article_list():
    # 获取文章数据
    page = request.args.get('page', type=int, default=1)
    per_page = request.args.get('limit', type=int, default=10)

    q = db.select(ArticleORM)

    # 类别筛选查询
    cid = request.args.get('cid', type=int, default=1)
    if cid in [2, 3, 4, 5, 6]:
        q = q.where(ArticleORM.category_id == cid)
        q = q.order_by(ArticleORM.id)

    status = request.args.get('status', type=int)
    if isinstance(status, int):
        q = q.where(ArticleORM.status == int(status))

    title = request.args.get('title')
    if title:
        q = q.where(ArticleORM.title.like(f'%{title}%'))

    q = q.order_by(ArticleORM.create_at.desc())
    # 分页查询
    pag = db.paginate(q, page=page, per_page=per_page, error_out=False)

    article_list = pag.items
    # 数据量大就需要分页
    return {
        'code': 0,
        'message': '获取文章数据成功',
        'count': pag.total,
        'page': pag.page,
        'limit': pag.per_page,
        'data': [article.to_dict() for article in article_list]
    }


# @admin_api.post('/article')
# def create_article():
#     return {'code': 0, 'message': '新增分类成功'}


@admin_api.get('/article/<int:aid>')
def get_article(aid):
    article = ArticleORM.query.get(aid)
    return {'code': 0, 'message': '获取文章信息成功', 'data': article.to_dict()}


@admin_api.put('/article/<int:aid>')
def update_article(aid):
    article = ArticleORM.query.get(aid)
    data = request.get_json()
    for k, v in data.items():
        setattr(article, k, v)
    article.save()
    return {'code': 0, 'message': '修改文章成功'}


@admin_api.delete('/article/<int:aid>')
def delete_article(aid):
    article: ArticleORM = ArticleORM.query.get(aid)
    # comments:CommentORM = CommentORM.query.get([comment for comment in this_article_comment_list])
    article.delete()
    # comments.delete()
    return {'code': 0, 'message': '删除文章成功'}


@admin_api.get('/cate/<int:cid>')
def get_cate(cid):
    cate = CategoryORM.query.get(cid)

    return {'code': 0, 'message': '获取分类信息成功', 'data': {'id': cate.id, 'name': cate.name}}


@admin_api.post('/cate')
def create_cate():
    name = request.json.get('name')
    cate = CategoryORM()
    cate.name = name
    cate.save()
    return {'code': 0, 'message': '新增分类成功'}


@admin_api.put('/cate/<int:cid>')
def update_cate(cid):
    # if cid in CategoryORM.id:
    cate = CategoryORM.query.get(cid)
    data = request.get_json()
    for k, v in data.items():
        setattr(cate, k, v)
    cate.save()
    return {'code': 0, 'message': '修改分类成功'}


# else:
#     return {'code': -1, 'message': '请先创建分类后修改'}


@admin_api.delete('/cate/<int:cid>')
def delete_cate(cid):
    cate = CategoryORM.query.get(cid)
    cate.delete()
    return {'code': 0, 'message': '删除分类成功'}


# @admin_api.get('/cate')
# def get_cate_list():
#     cate_list: list[CategoryORM] = CategoryORM.query.all()
#     # 数据量大就需要分页
#     return {
#         'code': 0,
#         'message': '获取分类数据成功',
#         'data': [{
#             'id': cate.id,
#             'name': cate.name,
#         } for cate in cate_list]
#     }


@admin_api.get('/cate')
def c():
    return render_template('admin/cate.html')